﻿using System;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Modules.CMS.Components;

namespace ASPNETVN.PORTAL.Modules.CMS.Controls
{
    public partial class GroupManager : AddItemControl
    {
       protected void btnAddGroup_Click(object sender, EventArgs e)
        {
            if (txtGroupName.Text.Length > 0)
            {
                Group group = new Group();
                string title = txtGroupName.Text.Trim();
                string desc = txtGroupDescription.Text.Trim();
                if (title.Length > 128)
                {
                    throw new Exception("Tiêu đề tối đa 128 ký tự.");

                }
                if (desc.Length > 512)
                {
                    throw new Exception("Mô tả tối đa 512 ký tự.");

                }
                else
                {
                    group.Title = title;
                    group.Description = desc;
                    group.ModuleID = this.ModuleID;
                    group.Insert();
                    gvGroups.DataBind();
                    txtGroupName.Text = txtGroupDescription.Text = string.Empty;
                }
            }
        }

        protected void hfModuleID_Init(object sender, EventArgs e)
        {
            hfModuleID.Value = this.ModuleID.ToString();
        }

        protected void gvGroups_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row = gvGroups.Rows[gvGroups.EditIndex];
            TextBox title = (TextBox)row.FindControl("TextBox2");
            TextBox desctiption = (TextBox)row.FindControl("TextBox1");
            if (title.Text.Length > 128)
            {
                e.Cancel = true;
                throw new Exception("Tiêu đề tối đa 128 ký tự.");
            }
            if (desctiption.Text.Length > 512)
            {
                e.Cancel = true;
                throw new Exception("Mô tả tối đa 512 ký tự.");
            }
        }
    }
}